{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit ImageUtils;

interface

uses PngImage;

  function LoadImage(const Filename: WideString): TPngObject; overload;
  function LoadImage(const Filename: WideString; Width, Height: Integer): TPngObject; overload;


implementation

uses Classes, GDIPOBJ, GDIPUTIL;

function LoadImage(const Filename: WideString): TPngObject;
var
  Bitmap: TGPBitmap;
  Stream: TMemoryStream;
  Encoding: TGUID;
begin
  // Set default result - failed to load
  Result := nil;

  try
    Bitmap := TGPBitmap.Create(Filename);
    try
      // We have now loaded image in its native format, so convert to PNG format
      GetEncoderClsid('image/png', Encoding);

      // Write the BMP format image to our memory stream
      Stream := TMemoryStream.Create;
      try
        Bitmap.Save(TStreamAdapter.Create(Stream), Encoding);
        Stream.Position := 0;

        // So far, so good, now read the stream into a PNG image
        Result := TPngObject.Create;
        Result.LoadFromStream(Stream);
      finally
        Stream.Free;
      end;
    finally
      Bitmap.Free;
    end;
  except
    // Ignore any exceptions whilst loading images
  end;
end;

function LoadImage(const Filename: WideString; Width, Height: Integer): TPngObject; overload;
var
  Bitmap, Thumb: TGPBitmap;
  ImgWidth, ImgHeight: Integer;
  Stream: TMemoryStream;
  Encoding: TGUID;
begin
  // Set default result - failed to load
  Result := nil;

  try
    Bitmap := TGPBitmap.Create(Filename);
    try
      ImgWidth := Bitmap.GetWidth;
      ImgHeight := Bitmap.GetHeight;

      // Does image need resizing to match desired size?
      if (Width <> ImgWidth) or (Height <> ImgHeight) then
      begin
        Thumb := TGPBitmap(Bitmap.GetThumbnailImage(Width, Height, nil, nil));
        Bitmap.Free;
        Bitmap := Thumb;
      end;

      // We have now loaded image in its native format, so convert to PNG format
      GetEncoderClsid('image/png', Encoding);

      // Write the BMP format image to our memory stream
      Stream := TMemoryStream.Create;
      try
        Bitmap.Save(TStreamAdapter.Create(Stream), Encoding);
        Stream.Position := 0;

        // So far, so good, now read the stream into a PNG image
        Result := TPngObject.Create;
        Result.LoadFromStream(Stream);
      finally
        Stream.Free;
      end;
    finally
      Bitmap.Free;
    end;
  except
    // Ignore any exceptions whilst loading images
  end;
end;


end.

